<?php

namespace app\modules\admin\models;

use Yii;

/**
 * This is the model class for table "role".
 *
 * @property int $id
 * @property string $name 角色名称
 * @property int $status 状态 1：有效 0：无效
 * @property int $create_at 创建时间
 * @property int $update_at 更新时间
 */
class Role extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'role';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['create_at', 'update_at'], 'integer'],
            [['name'], 'string', 'max' => 50],
            [['status'], 'string', 'max' => 1],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => 'Name',
            'status' => 'Status',
            'create_at' => 'Create At',
            'update_at' => 'Update At',
        ];
    }

    public static function formatList(&$list)
    {
        foreach ($list as &$info) {
            $info['create_at_date'] = $info['create_at'] ? date('Y-m-d H:i:s', $info['create_at']) : '-';
            $info['update_at_date'] = $info['update_at'] ? date('Y-m-d H:i:s', $info['update_at']) : '-';
        }
    }

    public static function getRoleNameList()
    {
        $roleNameList = [];
        $roleList = self::find()->where(['status' => 1])->asArray()->all();
        if ($roleList) {
            $roleIds = array_column($roleList, 'id');
            $roleNames = array_column($roleList, 'name');
            $roleNameList = array_combine($roleIds, $roleNames);
        }
        return $roleNameList;
    }

}
